#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>
#include <sstream>

using namespace std;

class TransWord {
public:
    TransWord(){}

    void buildMap(const string &filename){
        ifstream ifs(filename);
        if(!ifs.good()){
            cerr << "error" << endl;
            return;
        }

        string key;
        string line;
        //先读前面的key，后面的内容在按行读取成对应的value
        while(ifs >> key,getline(ifs,line)){
            string word;
            if(line.size()>0){
                word = line.substr(1);//后面第一个元素是空格，所以从第一个元素空格后面读
            }
            _um[key] = word;//这里是把word赋值给key对应的value;
        }
        ifs.close();
    }

    void translate(const string &filename){
        ifstream ifs(filename);
        if(!ifs.good())
        {
            cerr << "ifstream is not good" << endl;
            return;
        }

        string line;
        while(getline(ifs,line)){
            istringstream iss(line);
            string word;
            while(iss >> word){
                auto it = _um.find(word);
                if(it != _um.end()){
                    cout << it->second;
                }else{
                    cout << word;
                }
                cout << " ";
            }
            cout << endl;
        }
        ifs.close();

    }

private:
    unordered_map<string, string> _um;
};



void test(){
    TransWord tw;
    tw.buildMap("map.txt");
    tw.translate("file.txt");
}

int main()
{
    test();
    return 0;
}   
